package com.ruoyi.logistic.service.impl;

import com.github.pagehelper.PageHelper;
import com.ruoyi.logistic.domain.*;
import com.ruoyi.logistic.domain.vo.NetworkBillCollectNetVo;
import com.ruoyi.logistic.domain.vo.NetworkBillCollectSalesmanVo;
import com.ruoyi.logistic.mapper.NetworkBillCollectSalesmanMapper;
import com.ruoyi.logistic.mapper.NetworkBillDetailMapper;
import com.ruoyi.logistic.mapper.NetworkNameMapper;
import com.ruoyi.logistic.mapper.NetworkOrderSourceMapper;
import com.ruoyi.logistic.service.NetworkBillCollectNetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

@Service
public class NetworkBillCollectNetServiceImpl implements NetworkBillCollectNetService {
    @Autowired
    private NetworkBillDetailMapper networkBillDetailMapper;
    @Autowired
    private NetworkBillCollectSalesmanMapper networkBillCollectSalesmanMapper;
    @Autowired
    private NetworkOrderSourceMapper networkOrderSourceMapper;
    @Autowired
    private NetworkNameMapper networkNameMapper;

    @Override
    public NetworkBillCollectNet collectNetworkByDay(NetworkBillCollectNetVo networkBillCollectNetVo) {
        NetworkBillCollectNet networkBillCollectNet = new NetworkBillCollectNet();
        if (networkBillCollectNetVo.getCollectType().equals("1")){
//  按日
            networkBillCollectNet.setOrderTime(networkBillCollectNetVo.getStartTime() + "至" + networkBillCollectNetVo.getEndTime());
            networkBillCollectNet.setNetworkCollect(networkBillDetailMapper.selectNetworkCollectByTime(networkBillCollectNetVo.getStartTime(), networkBillCollectNetVo.getEndTime()));
            networkBillCollectNet.setSalesmanCollect(networkBillDetailMapper.selectSalesmanCollectByTime(networkBillCollectNetVo.getStartTime(), networkBillCollectNetVo.getEndTime()));
            networkBillCollectNet.setTotalTicket(networkBillDetailMapper.selectByTotalTicket(networkBillCollectNetVo.getStartTime(), networkBillCollectNetVo.getEndTime()));
            networkBillCollectNet.setTotalWeight(networkBillDetailMapper.selectTotalWeightByTime(networkBillCollectNetVo.getStartTime(), networkBillCollectNetVo.getEndTime()));
            if (networkBillCollectNetVo.getChannelType() == null){
                networkBillCollectNet.setChannelType("全部");
            }else {
                networkBillCollectNet.setChannelType(networkBillCollectNetVo.getChannelType());
            }
            if (networkBillCollectNetVo.getOrderSourceId() == null){
                networkBillCollectNet.setOrderSource("全部");
            }else {
                networkBillCollectNet.setOrderSource(networkOrderSourceMapper.selectNameById(networkBillCollectNetVo.getOrderSourceId()));
            }
            networkBillCollectNet.setProductType("全部");
            if (networkBillCollectNetVo.getNetworkNameId() == null){
                networkBillCollectNet.setNetworkName("全部");
            }else {
                networkBillCollectNet.setNetworkName(networkNameMapper.selectNameById(networkBillCollectNetVo.getNetworkNameId()));
            }
            networkBillCollectNet.setNetworkCode("12400");
        }
        if (networkBillCollectNetVo.getCollectType().equals("2")){
            System.out.println("====="+networkBillCollectNetVo.getCollectType() );
            networkBillCollectNet.setOrderTime(networkBillCollectNetVo.getStartMonth() + "至" + networkBillCollectNetVo.getEndMonth());
            networkBillCollectNet.setNetworkCollect(networkBillDetailMapper.selectNetworkCollectByMonth(networkBillCollectNetVo.getStartMonth(), networkBillCollectNetVo.getEndMonth()));
            networkBillCollectNet.setSalesmanCollect(networkBillDetailMapper.selectSalesmanCollectByMonth(networkBillCollectNetVo.getStartMonth(), networkBillCollectNetVo.getEndMonth()));
            networkBillCollectNet.setTotalTicket(networkBillDetailMapper.selectByTotalTicketByMonth(networkBillCollectNetVo.getStartMonth(), networkBillCollectNetVo.getEndMonth()));
            networkBillCollectNet.setTotalWeight(networkBillDetailMapper.selectTotalWeightByMonth(networkBillCollectNetVo.getStartMonth(), networkBillCollectNetVo.getEndMonth()));
            if (networkBillCollectNetVo.getChannelType() == null){
                networkBillCollectNet.setChannelType("全部");
            }else {
                networkBillCollectNet.setChannelType(networkBillCollectNetVo.getChannelType());
            }
            if (networkBillCollectNetVo.getOrderSourceId() == null){
                networkBillCollectNet.setOrderSource("全部");
            }else {
                networkBillCollectNet.setOrderSource(networkOrderSourceMapper.selectNameById(networkBillCollectNetVo.getOrderSourceId()));
            }
            networkBillCollectNet.setProductType("全部");
            if (networkBillCollectNetVo.getNetworkNameId() == null){
                networkBillCollectNet.setNetworkName("全部");
            }else {
                networkBillCollectNet.setNetworkName(networkNameMapper.selectNameById(networkBillCollectNetVo.getNetworkNameId()));
            }
            networkBillCollectNet.setNetworkCode("12400");
        }
        return networkBillCollectNet;
    }


    @Override
    public List<NetworkBillCollectSalesman> collectSalesmanByDay(NetworkBillCollectSalesmanVo networkBillCollectSalesmanVo) {
        PageHelper.startPage(networkBillCollectSalesmanVo.getPageDate());
        List<NetworkBillCollectSalesman> collectSalesmen = networkBillCollectSalesmanMapper.selectSalesmanByDay(networkBillCollectSalesmanVo);
        if (networkBillCollectSalesmanVo.getCollectType() == 1){
            for (NetworkBillCollectSalesman networkBillCollectSalesman : collectSalesmen) {
                networkBillCollectSalesman.setOrderTime(networkBillCollectSalesmanVo.getStartTime() + "至" + networkBillCollectSalesmanVo.getEndTime());
                if (networkBillCollectSalesmanVo.getChannelType() == null){
                    networkBillCollectSalesman.setChannelType("全部");
                }else {
                    networkBillCollectSalesman.setChannelType(networkBillCollectSalesmanVo.getChannelType());
                }
                if (networkBillCollectSalesmanVo.getOrderSourceId() == null){
                    networkBillCollectSalesman.setOrderSource("全部");
                }else {
                    networkBillCollectSalesman.setOrderSource(networkOrderSourceMapper.selectNameById(networkBillCollectSalesmanVo.getOrderSourceId()));
                }
                if (networkBillCollectSalesmanVo.getNetworkNameId() == null){
                    networkBillCollectSalesman.setNetworkName("全部");
                }else {
                    networkBillCollectSalesman.setNetworkName(networkNameMapper.selectNameById(networkBillCollectSalesmanVo.getNetworkNameId()));
                }
                networkBillCollectSalesman.setNetworkCode("");
            }
        }
        if (networkBillCollectSalesmanVo.getCollectType() == 2){
            for (NetworkBillCollectSalesman networkBillCollectSalesman : collectSalesmen) {
                networkBillCollectSalesman.setOrderTime(networkBillCollectSalesmanVo.getStartMonth() + "至" + networkBillCollectSalesmanVo.getEndMonth());
                if (networkBillCollectSalesmanVo.getChannelType() == null){
                    networkBillCollectSalesman.setChannelType("全部");
                }else {
                    networkBillCollectSalesman.setChannelType(networkBillCollectSalesmanVo.getChannelType());
                }
                if (networkBillCollectSalesmanVo.getOrderSourceId() == null){
                    networkBillCollectSalesman.setOrderSource("全部");
                }else {
                    networkBillCollectSalesman.setOrderSource(networkOrderSourceMapper.selectNameById(networkBillCollectSalesmanVo.getOrderSourceId()));
                }
                if (networkBillCollectSalesmanVo.getNetworkNameId() == null){
                    networkBillCollectSalesman.setNetworkName("全部");
                    networkBillCollectSalesman.setNetworkCode("");

                }else {
                    networkBillCollectSalesman.setNetworkName(networkNameMapper.selectNameById(networkBillCollectSalesmanVo.getNetworkNameId()));
                    networkBillCollectSalesman.setNetworkCode(networkNameMapper.selectCodeByName(networkBillCollectSalesmanVo.getNetworkNameId()));
                }

            }
        }
        return collectSalesmen;
    }
}
